//define(['js/module.js','jquery','common'],function(controllers,$,common){
    'use strict';
	angular.module('usercenter.tradeInfoSort', [])
    .controller('tradeInfoSortCtrl'
			,['$rootScope', '$scope', 'BizData', '$uibModalInstance','params', 'title'
			   , f])//控制器结束
    function f($rootScope, $scope, BizData, $uibModalInstance, params, title) {


   		var Model = this;
   		

   		
   		var options = {};
//		options.dataService = tradeInfoService;
		options.httpUrl = $rootScope.getServiceName();
   		options.reader = '/backcenter/tradeInfo/tree/query';
		options.writer = '/backcenter/tradeInfo/tree/save';
		options.deleter = '/backcenter/tradeInfo/tree/delete';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
//		options.defCols = {fID:{name:"fID",type:"String",label:""},systemCode:{name:"systemCode",type:"String",label:""},systemName:{name:"systemName",type:"String",label:""},systemFullID:{name:"systemFullID",type:"String",label:""},systemFullCode:{name:"systemFullCode",type:"String",label:""},systemFullName:{name:"systemFullName",type:"String",label:""},systemKind:{name:"systemKind",type:"String",label:""},systemDescribe:{name:"systemDescribe",type:"String",label:""},itemPrice:{name:"itemPrice",type:"Decimal",label:""},absoluteUrl:{name:"absoluteUrl",type:"String",label:""},nodeLevel:{name:"nodeLevel",type:"Integer",label:""},nodeKind:{name:"nodeKind",type:"String",label:""},sortNo:{name:"sortNo",type:"String",label:""},fParent:{name:"fParent",type:"String",label:""},status:{name:"status",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
   		options.defCols = {fID:{name:"fID",type:"String",label:""},tradeCode:{name:"tradeCode",type:"String",label:""},tradeName:{name:"tradeName",type:"String",label:""},tradeFullID:{name:"tradeFullID",type:"String",label:""},tradeFullCode:{name:"tradeFullCode",type:"String",label:""},tradeFullName:{name:"tradeFullName",type:"String",label:""},nodeLevel:{name:"nodeLevel",type:"Integer",label:""},nodeKind:{name:"nodeKind",type:"String",label:""},sortNo:{name:"sortNo",type:"String",label:""},fParent:{name:"fParent",type:"String",label:""},cUsercode:{name:"cUsercode",type:"String",label:""},cUsername:{name:"cUsername",type:"String",label:""},cDatetime:{name:"cDatetime",type:"Datetime",label:""},userId:{name:"userId",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = true;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.nodeSortRelation = "sortNo";
		options.pathRelation = 'systemFullID';
		options.directDeleteMode = false;
		options.limit = -1;
		options.orderBy = 'sortNo ASC';

   		options.onIndexChanging = Model.dataListIndexChanging;
   		options.onIndexChanged = Model.dataListIndexChanged;
   		options.onBeforeNew = null;
   		options.onAfterNew = Model.dataListAfterNew;
   		options.onBeforeDelete = null;
   		options.onAfterDelete = Model.dataListAfterDelete;
   		options.onValueChanged = Model.dataListValueChanged;
   		options.onDataChanged = Model.dataListDataChanged;
//                               		options.rowsWatcherFn = Model.rowsWatcherFn;
   		
   		$scope.dataList = new BizData(options);
//                               		$scope.dataList.refreshData();

//                               		$scope.dataList.deregIndex = $scope.$watch('dataList.currentIndex',$scope.dataList.watchCurrentIndex);
//                               		$scope.dataList.deregID = $scope.$watch('dataList.currentID',$scope.dataList.watchCurrentID);
   		//监视对象中的所有变动
   		$scope.dataList.deregRows = $scope.$watch('dataList.rows',$scope.dataList.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
//                               		$scope.dataList.deregCollection = $scope.$watch('dataList.rows',$scope.dataList.watchCollection,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
   		//////////////////////////////////////////////


		
		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
            Model.title = title;  
            Model.params = params;  

            if(Model.params.parentID==null){
        		//设置过滤条件
        		$scope.dataList.filter = "TradeInfo.fParent is null";//
            }else{
        		//设置过滤条件
        		$scope.dataList.filter = "TradeInfo.fParent='"+ Model.params.parentID +"'";//
            }
      	
       		$scope.dataList.refreshData();

        });  
		
        //置顶
        Model.btnMoveTopClick = function () {
        	var rowID = $scope.dataList.getCurrentID();
        	var index = $scope.dataList.getIndex(rowID);
        	if(rowID)
        	$scope.dataList.moveRowPro(0 - index, rowID);
        }
        //上移
        Model.btnMoveUpClick = function () {
        	var rowID = $scope.dataList.getCurrentID();
        	if(rowID)
        	$scope.dataList.moveRowPro(-1, rowID);
        }
        //下移
        Model.btnMoveDownClick = function () {
        	var rowID = $scope.dataList.getCurrentID();
        	if(rowID)
        	$scope.dataList.moveRowPro(1, rowID);
        }
        //置底
        Model.btnMoveBottomClick = function () {
        	var rowID = $scope.dataList.getCurrentID();
        	var index = $scope.dataList.getIndex(rowID);
        	var count = $scope.dataList.getTotal();
        	if(rowID)
        	$scope.dataList.moveRowPro(count - index - 1, rowID);
        }
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	
	    	//循环data
			for(var index=0;index<$scope.dataList.rows.length;index++){
				var rowID = $scope.dataList.rows[index].fID.value;
				data.push(rowID);
			}
	    	
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    
		//////////////////////对话框使用结束//////////////////////////////////////
	    



	    return Model;
	    
	}	
	
//});
